<?php
class User extends CI_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model('user_model');
	}

	public function login()
	{
		//user input
		$mail = $this->input->post('mail');
		$password = $this->input->post('password');
		$is_remember = $this->input->post('remember') != null;
		if(!$mail || !$password) redirect('/?error='.Variable::ERROR_INPUT);

		//get user info
		$user_info = $this->user_model->get_user_by_mail_password($mail, $password);
		
		//authenticate
		if(!$user_info) redirect('/?error='.Variable::ERROR_USER);
		else if($user_info->activated == 0)
		{
			$data['mail'] = $mail;
			$data['from'] = Variable::MAIL_LOGIN;
			$this->load->library('utils/mail_utils');
			$this->load->view('user/send_mail', $data);	
		}
		else
		{
			if($is_remember)
			{
				$this->session->set_userdata(Variable::SESSION_USER_ID, $user_info->userId);
				$this->session->set_userdata(Variable::SESSION_USER_NAME, $user_info->mail);
			}
			else
			{
				session_start();
				$_SESSION[Variable::SESSION_USER_ID] = $user_info->userId;
				$_SESSION[Variable::SESSION_USER_NAME] = $user_info->mail;	
			}
			redirect('record');
		}
	}

	public function logout()
	{
		$this->session->unset_userdata(Variable::SESSION_USER_ID);
		$this->session->unset_userdata(Variable::SESSION_USER_NAME);
		
		session_destroy();
		redirect('');
	}
	
	public function change_password()
	{
		//session
		$mail = $this->session->userdata(Variable::SESSION_USER_NAME);
		if(!$mail)
		{
			$data['result'] = 'false';
			$this->load->view('user/result', $data);
			return ;
		}
		
		//validate
		$password = $this->input->get_post('password');
		$user_info = $this->user_model->get_user_by_mail_password($mail, $password);
		if(!$user_info)
		{
			$data['result'] = 'false';
			$this->load->view('user/result', $data);
			return ;
		}
		
		//change password
		$new_password = $this->input->get_post('new_password');
		$this->user_model->change_password($mail, $new_password);
		
		$data['result'] = 'true';
		$this->load->view('user/result', $data);
	}
	
	public function check()
	{
		$mail = $this->input->get_post('mail');
		
		$user = $this->user_model->get_user_by_mail($mail);
		$data['result'] = !$user ? 'true' : 'false';
		$this->load->view('user/result', $data);
	}
	
	public function register()
	{
		//get user input
		$mail = $this->input->get_post('mail');
		$password = $this->input->get_post('password');
		
		//get user info
		$user = $this->user_model->get_user_by_mail($mail);
		if($user)
		{
			redirect('/?error='.Variable::ERROR_USER);		
		}
		else
		{
			$user_id = $this->user_model->add_user($mail, $password);	
			
			//send mail
			$this->load->library('utils/mail_utils');
			$this->mail_utils->send_mail($mail);
		
			//load view
			$data['mail'] = $mail;
			$data['from'] = Variable::MAIL_REGISTER;
			$this->load->view('user/send_mail', $data);
		}
	}
	
	public function send_mail($key)
	{
		$user = $this->user_model->get_user_by_key($key);
		
		if($user)
		{
			$this->load->library('utils/mail_utils');
			$this->mail_utils->send_mail($user->mail);
			
			$data['mail'] = $user->mail;
			$data['from'] = Variable::MAIL_REGISTER_SEND;
			$this->load->view('user/send_mail', $data);
		}
		else 
		{
			redirect('/?error='.Variable::ERROR_NOT_EXIST);			
		}
	}
	
	public function validate($key)
	{
		$this->user_model->validate_user($key);
		
		$data['from'] = Variable::MAIL_SUCCESS_VALIDATE;
		$this->load->view('user/mail_success', $data);
	}
	
	public function forgot_password()
	{
		$data['from'] = '';
		$this->load->view('user/forgot_password', $data);
	}
	
	public function send_forgot_mail()
	{
		$this->load->	model('forgot_model');
		$this->load->library('utils/mail_utils');
		
		$mail = $this->input->get_post('mail');
		$change_key = $this->user_utils->get_change_key($mail);
		
		$this->forgot_model->del_forgot($mail);
		$this->forgot_model->add_forgot($mail, $change_key);
		$this->mail_utils->send_forgot_mail($mail, $change_key);
		
		$data['mail'] = $mail;
		$data['from'] = Variable::MAIL_FORGOT;
		$this->load->view('user/send_mail', $data);
	}
	
	public function reset($key)
	{
		$data['key'] = $key;
		$this->load->view('user/reset', $data);
	}
	
	public function reset_password()
	{
		$this->load->model('forgot_model');
		
		$change_key = $this->input->get_post('changeKey');
		$password = $this->input->get_post('password');
		$forgot = $this->forgot_model->get_forgot($change_key);
		if(!$forgot) return;
		
		$mail = $forgot->mail;
		$is_expired = $forgot->expireTime < date('Y-m-d H:i:s');
		if($is_expired)
		{
			$data['from'] = Variable::MAIL_FALSE_EXPIRED;
			$this->load->view('user/forgot_password', $data);	
		}
		else
		{
			$this->forgot_model->del_forgot($mail);
			$this->user_model->change_password($mail, $password);

			$data['from'] = Variable::MAIL_SUCCESS_RESET;
			$this->load->view('user/mail_success', $data);
		}
	}
}
?>